package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:  用车状况多选
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/9 17:25
 */
public class CarStatusMultipleFragment extends BaseListFragment {
  public static final String EXTRA_CODES =
      CarStatusMultipleFragment.class.getSimpleName() + "EXTRA_CODES";
  public static final String EXTRA_NAMES =
      CarStatusMultipleFragment.class.getSimpleName() + "EXTRA_NAMES";
  private ArrayList<ZTreeNode> mList;
  private CarStatusListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("用车状况");
  }

  @Override public String getUrl() {

    return ApiConst.URL_DATA_DICTIONARY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("dictFlg", "carStatus");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {

    ZTreeNode[] list = new Gson().fromJson(json, ZTreeNode[].class);
    mList = new ArrayList<>();
    for (ZTreeNode aList : list) {
      mList.add(aList);
    }
    HashMap<ZTreeNode, Boolean> map = new HashMap<>();
    mAdapter = new CarStatusListAdapter(getActivity(), mList, map);
    setListAdapter(mAdapter);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        getActivity().finish();
        return true;

      case R.id.menu_item_confirm:
        if (mList == null) return false;
        StringBuilder codes = new StringBuilder();
        StringBuilder names = new StringBuilder();
        for (ZTreeNode node : mList) {
          if (mAdapter.isCheckedItem(node)) {
            codes.append(node.getId()).append(MyStringUtil.COMMA);
            names.append(node.getName()).append(MyStringUtil.COMMA);
          }
        }
        Intent i = new Intent();
        i.putExtra(EXTRA_CODES, MyStringUtil.substring(codes.toString(), 0, codes.length() - 1));
        i.putExtra(EXTRA_NAMES, MyStringUtil.substring(names.toString(), 0, names.length() - 1));
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();
        return true;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }
}
